package com.example.musicplayer.activity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.musicplayer.R;
import com.example.musicplayer.commonUtils.DisplayUtil;
import com.example.musicplayer.commonUtils.DrawUtil;
import com.example.musicplayer.dialogTools.SortMusicDialog;
import com.example.musicplayer.musicTools.MusicListType;
import com.example.musicplayer.musicTools.MusicOp;
import com.example.musicplayer.musicTools.MusicSortType;
import com.example.musicplayer.musicClass.MusicInfo;
import com.example.musicplayer.musicClass.MusicMenu;
import com.example.musicplayer.service.MusicService;
import com.example.musicplayer.settings.MusicPlayerSettings;
import com.example.musicplayer.user.User;
import com.example.musicplayer.view.MusicSimpleDisplay;
import com.example.musicplayer.view.NewScrollView;
import com.example.musicplayer.view.SimpleDisplayView;
import com.example.musicplayer.windowTools.WindowTools;

import java.util.ArrayList;
import java.util.List;

public class MusicMenuDetailsActivity extends BaseActivity implements View.OnClickListener {
    public static final String TAG = "MusicMenuDetailActivity";
    public static final int RESULT_CODE = 200;
    private ImageView mImageViewLocation;
    private ImageView mImageViewLocation1;
    private LinearLayout mMusicContainer;
    private View mInfoTab;
    private ImageView mImageViewAlbum;
    private ImageView mImageViewUser;
    private TextView mTextViewMenuName;
    private View mFooterView;
    private NewScrollView mScrollView;
    private List<Integer> mMusic = new ArrayList<Integer>();
    private final List<MusicSimpleDisplay> mDisplay = new ArrayList<MusicSimpleDisplay>();
    private String mMusicMenuName;
    private Bitmap mBitmapAlbum;
    private int mScrollDistanceLimit;
    private int mRealScreenHeight;
    private int mScrollContentHeight;
    //对歌单是否调整
    private boolean mStateChanged = false;
    //是否更新整个页面
    private boolean mShouldUpdate = true;
    private boolean mIsToBottom = true;
    private MusicService.MusicBinder mMusicBinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music_menu_details);
        if ((mMusicBinder = getMusicServiceBinder()) == null) {
            finish();
        }
        WindowTools.setStatusBarTransparent(getWindow());
        initView();
        addListener();
        Intent intent = getIntent();
        mMusicMenuName = intent.getStringExtra("musicMenuName");
        mScrollDistanceLimit = (int) getResources().getDimension(R.dimen.activity_music_menu_details_info_tab_height) -
                (int) getResources().getDimension(R.dimen.activity_music_menu_details_toolbar_height);
        setEnableMusicListener(true);
        //show music playing info tab
        setEnableMusicTabView(true);
        registerMusicListener(mMusicBinder);
        checkAndUpdateMusicMenu();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mShouldUpdate) {
            updateAll();
            mShouldUpdate = false;
        }
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (mMusicBinder != null && hasFocus) {
            registerMusicListener(mMusicBinder);
            mRealScreenHeight = DisplayUtil.getRealScreenSize(this)[1];
        }
    }

    @Override
    public void onBackPressed() {
        finishActivity();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        Log.d(TAG, "onActivityResult(): requestCode is " + requestCode + ", resultCode is " + resultCode);
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == MusicListManageActivity.RESULT_CODE && data != null) {
            if (data.getBooleanExtra("result", true)) {
                mStateChanged = true;
                //更新视图
                updateAll();
            }
        }
        if (resultCode == EditMusicMenuDetailsActivity.RESULT_CODE && data != null) {
            if (data.getBooleanExtra("result", true)) {
                mStateChanged = true;
                String newName = data.getStringExtra("musicMenuName");
                updateIntroductionChanged(newName);
                updateView();
            }
        }
    }

    private void checkAndUpdateMusicMenu() {
        MusicMenu musicMenu = MusicInfo.getMusicMenuByName(mMusicMenuName);
        if (musicMenu != null) {
            mStateChanged = MusicInfo.checkAndUpdateMusicMenu(musicMenu);
        }
    }

    private void initView() {
        mImageViewLocation = findViewById(R.id.imageview_music_menu_details_location);
        mImageViewLocation1 = findViewById(R.id.imageview_music_menu_details_location1);
        mMusicContainer = findViewById(R.id.linear_layout_music_list_container);
        mInfoTab = findViewById(R.id.relative_layout_info_tab);
        mImageViewAlbum = findViewById(R.id.imageview_music_menu_details_album);
        mImageViewUser = findViewById(R.id.imageview_music_menu_details_user);
        mTextViewMenuName = findViewById(R.id.textview_music_menu_details_menu_name);
        mScrollView = findViewById(R.id.scrollview_content);
        //设置轮廓
        DisplayUtil.setRoundRectOutline(mImageViewAlbum,
                0,
                0,
                (int) getResources().getDimension(R.dimen.activity_music_menu_details_image_size),
                (int) getResources().getDimension(R.dimen.activity_music_menu_details_image_size),
                (int) getResources().getDimension(R.dimen.big_image_radius));
        DisplayUtil.setRoundOutline(mImageViewUser,
                0,
                0,
                (int) getResources().getDimension(R.dimen.user_photo_size_small),
                (int) getResources().getDimension(R.dimen.user_photo_size_small));
        //set current user's name
        ((TextView) findViewById(R.id.textview_current_user_name)).setText(User.getInstance().getUserName());
        setFooterView();
    }

    @SuppressLint("InflateParams")
    private void setFooterView() {
        mFooterView = LayoutInflater.from(this).inflate(R.layout.normal_footer_view, null);
        mFooterView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT
                , (int) getResources().getDimension(R.dimen.normal_footer_view_height)));
        mMusicContainer.addView(mFooterView);
    }

    private void addListener() {
        findViewById(R.id.imageview_music_menu_details_play).setOnClickListener(this);
        findViewById(R.id.textview_music_menu_details_play_info).setOnClickListener(this);
        findViewById(R.id.imageview_music_menu_details_manage).setOnClickListener(this);
        findViewById(R.id.imageview_music_menu_details_sort).setOnClickListener(this);
        findViewById(R.id.imageview_music_menu_details_add).setOnClickListener(this);
        mImageViewLocation.setOnClickListener(this);
        findViewById(R.id.imageview_music_menu_details_play1).setOnClickListener(this);
        findViewById(R.id.textview_music_menu_details_play_info1).setOnClickListener(this);
        findViewById(R.id.imageview_music_menu_details_manage1).setOnClickListener(this);
        findViewById(R.id.imageview_music_menu_details_sort1).setOnClickListener(this);
        findViewById(R.id.imageview_music_menu_details_add1).setOnClickListener(this);
        mImageViewLocation1.setOnClickListener(this);
        findViewById(R.id.imageview_music_menu_details_finish).setOnClickListener(this);
        findViewById(R.id.imageview_music_menu_details_menu).setOnClickListener(this);
        findViewById(R.id.search_music).setOnClickListener(this);
        findViewById(R.id.button_add_music).setOnClickListener(this);
        findViewById(R.id.imageview_music_menu_details_collect).setOnClickListener(this);
        findViewById(R.id.imageview_music_menu_details_comment).setOnClickListener(this);
        findViewById(R.id.imageview_music_menu_details_share).setOnClickListener(this);
        mImageViewAlbum.setOnClickListener(this);
        findViewById(R.id.textview_music_menu_details_collect).setOnClickListener(this);
        findViewById(R.id.textview_music_menu_details_comment).setOnClickListener(this);
        findViewById(R.id.textview_share).setOnClickListener(this);
        mTextViewMenuName.setOnClickListener(this);
        findViewById(R.id.textview_music_menu_details_view1).setOnClickListener(this);
        setScrollViewListener();
    }

    private void addListener(final MusicSimpleDisplay display) {
        display.getView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                doClickPlay(display.getNumber() - 1);
            }
        });
        display.getView().setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                doClickManage();
                return true;
            }
        });
        display.getImageViewVideo().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        display.getImageViewMore().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    private void setScrollViewListener() {
        mScrollView.setOnScrollListener(new NewScrollView.OnScrollListener() {
            @Override
            public void onScrollChanged(int y, int oldY) {
                if (y != oldY) {
                    updateToolbar();
                }
                int[] location = new int[2];
                mFooterView.getLocationOnScreen(location);
                if (location[1] <= mRealScreenHeight) {
                    setLocationState(false);
                } else if (y <= 0) {
                    setLocationState(true);
                }
            }
        });
    }

    //更新整个页面
    private void updateAll() {
        updateStateChanged();
        updateMusicView();
    }

    //增删或调整歌曲顺序后，更新
    private void updateStateChanged() {
        updateView();
    }

    //更新视图显示
    private void updateView() {
        MusicMenu musicMenu = MusicInfo.getMusicMenuByName(mMusicMenuName);
        //歌单中歌曲数量
        int musicCounts = musicMenu == null ? 0 : musicMenu.getCounts();
        mScrollContentHeight = mInfoTab.getHeight();
        if (musicCounts == 0) {
            findViewById(R.id.linear_layout_music_menu_details_empty).setVisibility(View.VISIBLE);
            mFooterView.setVisibility(View.GONE);
            findViewById(R.id.relative_layout_control_tab_move).setVisibility(View.GONE);
        } else {
            findViewById(R.id.linear_layout_music_menu_details_empty).setVisibility(View.GONE);
            mFooterView.setVisibility(View.VISIBLE);
            findViewById(R.id.relative_layout_control_tab_move).setVisibility(View.VISIBLE);
            //显示歌单音乐数量
            String text = getResources().getString(R.string.play_all_music) + "(" + musicCounts + ")";
            ((TextView) findViewById(R.id.textview_music_menu_details_play_info)).setText(text);
            ((TextView) findViewById(R.id.textview_music_menu_details_play_info1)).setText(text);
        }
        if (musicCounts >= MusicPlayerSettings.getLeastMusicCountsUseLocation()) {
            mImageViewLocation.setVisibility(View.VISIBLE);
            mImageViewLocation1.setVisibility(View.VISIBLE);
        } else {
            mImageViewLocation.setVisibility(View.GONE);
            mImageViewLocation1.setVisibility(View.GONE);
        }
        mTextViewMenuName.setText(mMusicMenuName);
        setAlbumBitmap();
        setTabBackground();
        updateMusicMenuIntroduction();
        updateToolbar();
        updateMusicMenuListenCounts();
    }

    //设置歌单图片
    private void setAlbumBitmap() {
        //获取歌单封面图
        MusicMenu musicMenu = MusicInfo.getMusicMenuByName(mMusicMenuName);
        if (musicMenu == null)
            mBitmapAlbum = DrawUtil.getDefaultMusicMenuBitmap();
        else
            mBitmapAlbum = musicMenu.getBitmap();
        //set photo for current user
        if (User.getInstance() == null) {
            mImageViewUser.setImageBitmap(mBitmapAlbum);
        } else {
            mImageViewUser.setImageBitmap(User.getInstance().getUserPhoto());
        }
        //set bitmap for current music menu
        Bitmap bitmap = DrawUtil.copyBitmap(mBitmapAlbum);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.parseColor("#44000000"));
        mImageViewAlbum.setImageBitmap(bitmap);
    }

    //更新播放次数
    private void updateMusicMenuListenCounts() {
        MusicMenu musicMenu = MusicInfo.getMusicMenuByName(mMusicMenuName);
        if (musicMenu != null) {
            int listenCounts = musicMenu.getListenCounts();
            ((TextView) findViewById(R.id.textview_music_menu_details_listen_counts)).setText(String.valueOf(listenCounts));
        }
    }

    //更新简介
    private void updateMusicMenuIntroduction() {
        MusicMenu musicMenu = MusicInfo.getMusicMenuByName(mMusicMenuName);
        TextView introduction = findViewById(R.id.textview_music_menu_details_introduction);
        if (musicMenu != null) {
            String text = musicMenu.getIntroduction();
            if (text != null) {
                introduction.setText(text);
            }
        }
        introduction.setTextColor(DrawUtil.getBodyTextColorOfPicture(mBitmapAlbum, DrawUtil.VIBRANT));
    }

    //更新toolbar显示
    private void updateToolbar() {
        if (mScrollView.getScrollY() >= mScrollDistanceLimit) {
            findViewById(R.id.relative_layout_control_tab).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.relative_layout_control_tab).setVisibility(View.GONE);
        }
        float distance = mScrollView.getScrollY();
        //toolbar的透明度
        int alpha = (int) (distance * 1.1f / (float) mScrollDistanceLimit * 255);
        //toolbar文字的透明度
        float alphaText = distance * 2 / (float) mScrollDistanceLimit;
        alpha = alpha < 0 ? 0 : Math.min(alpha, 255);
        alphaText = alphaText < 0 ? 0 : Math.min(alphaText, 2);
        findViewById(R.id.toolbar_music_menu_details).getBackground().setAlpha(alpha);
        TextView toolbarText = findViewById(R.id.textview_music_menu_details_toolbar);
        if (alphaText <= 1.0f) {
            toolbarText.setText(R.string.music_menu);
            toolbarText.setAlpha(1 - alphaText);
        } else {
            toolbarText.setText(mMusicMenuName);
            toolbarText.setAlpha(alphaText - 1);
        }
    }

    //设置顶部栏的背景色
    private void setTabBackground() {
        Bitmap bitmap = DrawUtil.getColorBitmap(Color.WHITE);
        int startColor = DrawUtil.getMainColorOfPicture(mBitmapAlbum, DrawUtil.DARK_VIBRANT);
        int endColor = DrawUtil.getMainColorOfPicture(mBitmapAlbum, DrawUtil.VIBRANT);
        bitmap = DrawUtil.getSlantedGradientBitmap(bitmap, startColor, endColor);
        mInfoTab.setBackground(new BitmapDrawable(getResources(), bitmap));
        findViewById(R.id.toolbar_music_menu_details).setBackground(new BitmapDrawable(getResources(), bitmap));
    }

    //在末尾插入歌曲项视图
    private void addNewMusicView(int index, boolean isListenCountsVisible) {
        MusicSimpleDisplay display = new MusicSimpleDisplay(SimpleDisplayView.TYPE_DISPLAY, index);
        //note that the footer view is visible
        mMusicContainer.addView(display.getView(), mMusicContainer.getChildCount() - 1);
        mDisplay.add(display);
        addListener(display);
        display.update(MusicInfo.getMusicInfoById(mMusic.get(index - 1)));
        display.enableListenCountsInfo(isListenCountsVisible);
        display.markAsPlaying((mMusicBinder != null) && (mMusic.get(index - 1) == mMusicBinder.getId()));
    }

    //更新歌曲列表项视图
    private void updateMusicView() {
        MusicMenu musicMenu = MusicInfo.getMusicMenuByName(mMusicMenuName);
        //收听次数是否可见
        boolean isVisible = musicMenu != null && (musicMenu.getMusicSortType() == MusicSortType.SORT_LISTEN_COUNTS);
        //获取排序结果
        mMusic = MusicOp.sort(mMusicMenuName);
        int num = mMusic.size() - mDisplay.size();
        if (num > 0) {
            int index = mDisplay.size();
            for (int i = 1; i <= num; i++) {
                addNewMusicView(index + i, isVisible);
            }
        }
        if (num < 0) {
            for (int i = -1; i >= num; i--) {
                mDisplay.remove(mDisplay.size() - 1);
                //ignore the footer view
                mMusicContainer.removeViewAt(mMusicContainer.getChildCount() - 1 - 1);
            }
        }
        if (num <= 0) {
            for (int i = 0; i < mMusic.size(); i++) {
                mDisplay.get(i).update(MusicInfo.getMusicInfoById(mMusic.get(i)));
                mDisplay.get(i).enableListenCountsInfo(isVisible);
                mDisplay.get(i).markAsPlaying((mMusicBinder != null) && (mMusic.get(i) == mMusicBinder.getId()));
            }
        }
        if (mDisplay.size() > 0 && mInfoTab != null) {
            mFooterView.setVisibility(View.VISIBLE);
            mScrollContentHeight = mInfoTab.getHeight();
            mScrollContentHeight += findViewById(R.id.relative_layout_control_tab_move).getHeight()
                    + mDisplay.get(0).getViewHeight() * mDisplay.size();
            if (mFooterView.getVisibility() == View.VISIBLE) {
                mScrollContentHeight += mFooterView.getHeight();
            }
        } else {
            mFooterView.setVisibility(View.GONE);
            mScrollContentHeight = 0;
        }
    }

    //歌单编辑简介，更新信息
    private void updateIntroductionChanged(String newName) {
        if (newName != null)
            mMusicMenuName = newName;
        mTextViewMenuName.setText(mMusicMenuName);
        updateMusicMenuIntroduction();
    }

    //播放歌单全部歌曲
    private void doClickPlay(int index) {
        if (mMusic.size() > 0) {
            if (index < 0 || index >= mMusic.size()) {
                index = 0;
            }
            MusicInfo clickMusic = mDisplay.get(index).getMusicInfo();
            if (mMusicBinder != null && clickMusic != null) {
                if (clickMusic.getId() != mMusicBinder.getId() || mMusicBinder.getMusicListType() != MusicListType.TYPE_MENU) {
                    mMusicBinder.start(MusicListType.TYPE_MENU, MusicInfo.getMusicMenuByName(mMusicMenuName)
                            , mDisplay.get(index).getMusicInfo());
                } else if (!mMusicBinder.isPlaying()) {
                    mMusicBinder.start();
                }
                if (MusicPlayerSettings.isAutoIntoPlayScreen()) {
                    jumpToMusicPlayActivity();
                }
            }
        }
    }

    //管理此歌单
    private void doClickManage() {
        Intent intent = new Intent(this, MusicListManageActivity.class);
        intent.putExtra("musicListType", MusicListType.TYPE_MENU);
        intent.putExtra("musicMenuName", mMusicMenuName);
        jumpToActivity(intent);
    }

    //点击歌曲排序选择功能
    private void doClickSort() {
        final MusicMenu musicMenu = MusicInfo.getMusicMenuByName(mMusicMenuName);
        //获取歌单的当前排序类型
        MusicSortType type = musicMenu == null ? MusicSortType.SORT_DEFAULT : musicMenu.getMusicSortType();
        //构造排序选择对话框
        SortMusicDialog sortMusicDialog = new SortMusicDialog(this, type);
        final Dialog dialog = sortMusicDialog.getDialog();
        View sortDefault = sortMusicDialog.getSortDefault();
        View sortDefaultReverse = sortMusicDialog.getSortDefaultReverse();
        View sortTitle = sortMusicDialog.getSortTitle();
        View sortArtist = sortMusicDialog.getSortArtist();
        View sortListenCounts = sortMusicDialog.getSortListenCounts();
        View cancel = sortMusicDialog.getCancel();
        //监听事件
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        sortDefault.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (musicMenu != null)
                    musicMenu.setMusicSortType(MusicSortType.SORT_DEFAULT);
                updateMusicView();
                dialog.dismiss();
            }
        });
        sortDefaultReverse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (musicMenu != null)
                    musicMenu.setMusicSortType(MusicSortType.SORT_DEFAULT_REVERSE);
                updateMusicView();
                dialog.dismiss();
            }
        });
        sortTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (musicMenu != null)
                    musicMenu.setMusicSortType(MusicSortType.SORT_TITLE);
                updateMusicView();
                dialog.dismiss();
            }
        });
        sortArtist.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (musicMenu != null)
                    musicMenu.setMusicSortType(MusicSortType.SORT_ARTIST);
                updateMusicView();
                dialog.dismiss();
            }
        });
        sortListenCounts.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (musicMenu != null)
                    musicMenu.setMusicSortType(MusicSortType.SORT_LISTEN_COUNTS);
                updateMusicView();
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    //从本地音乐中添加歌曲到歌单
    private void doClickAdd() {
        Intent intent = new Intent(this, MusicListManageActivity.class);
        intent.putExtra("musicListType", MusicListType.TYPE_LOCAL);
        intent.putExtra("musicMenuName", mMusicMenuName);
        jumpToActivity(intent);
    }

    private void doClickAddTo() {
        doClickAdd();
    }

    private void doClickLocation() {
        int firstIndex = firstItemIndex();
        int markedIndex = markedItemIndex();
        if (mIsToBottom) {
            if (firstIndex >= markedIndex) {
                mScrollView.smoothScrollTo(0, mScrollContentHeight);
            } else {
                scrollToMarkedItem();
            }
        } else {
            if (firstIndex <= markedIndex) {
                mScrollView.smoothScrollTo(0, 0);
            } else {
                scrollToMarkedItem();
            }
        }
    }

    private void doClickFinish() {
        finishActivity();
    }

    private void doClickMenu() {
    }

    private void doClickCollect() {
    }

    private void doClickComment() {
    }

    private void doClickShare() {
    }

    private void doClickIntroduction() {
    }

    //编辑歌单详情
    private void doClickEditIntroduction() {
        Intent intent = new Intent(this, EditMusicMenuDetailsActivity.class);
        intent.putExtra("musicMenuName", mMusicMenuName);
        jumpToActivity(intent);
    }

    //跳转到其它activity
    private void jumpToActivity(Intent intent) {
        startActivityForResult(intent, 1);
        setPendingTransition(R.anim.activity_vertical_enter, R.anim.fake_anim);
    }

    //结束activity
    private void finishActivity() {
        Intent intent = new Intent();
        intent.putExtra("result", mStateChanged);
        this.setResult(RESULT_CODE, intent);
        finish();
        setPendingTransition(R.anim.slow_activity_enter, R.anim.normal_activity_exit);
    }

    //计算第一个可见item项的索引
    private int firstItemIndex() {
        if (mMusic.size() == 0) {
            return 0;
        } else {
            int itemHeight = mDisplay.get(0).getViewHeight();
            int rest = (int) mScrollView.getScrollY() - mScrollDistanceLimit;
            if (rest <= 0) {
                return 0;
            } else {
                return rest / itemHeight;
            }
        }
    }

    //获取当前播放歌曲的索引
    private int markedItemIndex() {
        if (mDisplay.size() > 0) {
            for (int i = 0; i < mDisplay.size(); i++) {
                if (mDisplay.get(i).isMarkAsPlaying()) {
                    return i;
                }
            }
        }
        return -1;
    }

    private void setLocationState(boolean isToBottom) {
        mIsToBottom = isToBottom;
        if (mIsToBottom) {
            mImageViewLocation.setImageResource(R.drawable.icon_to_bottom);
            mImageViewLocation1.setImageResource(R.drawable.icon_to_bottom);
        } else {
            mImageViewLocation.setImageResource(R.drawable.icon_to_top);
            mImageViewLocation1.setImageResource(R.drawable.icon_to_top);
        }
    }

    //滑动到标记的歌曲处
    private void scrollToMarkedItem() {
        int markedIndex = Math.max(markedItemIndex(), 0);
        int distance = mScrollDistanceLimit + mDisplay.get(0).getViewHeight() * markedIndex;
        mScrollView.smoothScrollTo(0, distance);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.imageview_music_menu_details_play:
            case R.id.imageview_music_menu_details_play1:
            case R.id.textview_music_menu_details_play_info:
            case R.id.textview_music_menu_details_play_info1:
                doClickPlay(0);
                break;
            case R.id.imageview_music_menu_details_manage:
            case R.id.imageview_music_menu_details_manage1:
                doClickManage();
                break;
            case R.id.imageview_music_menu_details_sort:
            case R.id.imageview_music_menu_details_sort1:
                doClickSort();
                break;
            case R.id.imageview_music_menu_details_add:
            case R.id.imageview_music_menu_details_add1:
                doClickAddTo();
                break;
            case R.id.imageview_music_menu_details_location:
            case R.id.imageview_music_menu_details_location1:
                doClickLocation();
                break;
            case R.id.button_add_music:
                doClickAdd();
                break;
            case R.id.imageview_music_menu_details_finish:
                doClickFinish();
                break;
            case R.id.imageview_music_menu_details_menu:
                doClickMenu();
                break;
            case R.id.search_music:
                Intent intent = new Intent(this, MusicSearchActivity.class);
                intent.putExtra(MusicSearchActivity.TYPE, MusicListType.TYPE_MENU.toString());
                intent.putExtra(MusicSearchActivity.MUSIC_MENU, mMusicMenuName);
                jumpToActivity(intent);
                break;
            case R.id.imageview_music_menu_details_collect:
            case R.id.textview_music_menu_details_collect:
                doClickCollect();
                break;
            case R.id.imageview_music_menu_details_comment:
            case R.id.textview_music_menu_details_comment:
                doClickComment();
                break;
            case R.id.imageview_music_menu_details_share:
            case R.id.textview_share:
                doClickShare();
                break;
            case R.id.imageview_music_menu_details_album:
            case R.id.textview_music_menu_details_menu_name:
                doClickIntroduction();
                break;
            case R.id.textview_music_menu_details_view1:
                doClickEditIntroduction();
                break;
        }
    }

    @Override
    public void onMusicPrepared(MusicInfo musicInfo) {
        if (musicInfo != null && mDisplay != null) {
            for (int i = 0; i < mDisplay.size(); i++) {
                MusicInfo temp = mDisplay.get(i).getMusicInfo();
                mDisplay.get(i).markAsPlaying((temp != null) && (temp.getId() == musicInfo.getId()));
            }
        }
    }

    @Override
    public void onMusicEmpty(MusicInfo musicInfo) {
        if (mMusic != null && musicInfo != null) {
            for (int i = 0; i < mMusic.size(); i++)
                if (mMusic.get(i) == musicInfo.getId() && mDisplay != null && mDisplay.size() > i)
                    mDisplay.get(i).markAsPlaying(false);
        }
    }
}